# Create your views here.
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext

from models import Tournament, Category, Club
from models import ClubForm

def home_hms(request):
    return render_to_response('home.html',context_instance=RequestContext(request))
    
def listing(request, model):
    obj_list = model.objects.all()
    template_name = '%s_list.html' % model.__name__.lower()
    return render_to_response(template_name,{'object_list':obj_list},context_instance=RequestContext(request))

def category_challengues(request, categ, tournament):
    categ_name = Category.objects.filter(category_code=categ) # must be a valid Category
     
    if categ_name:
        if tournament:
            categ_list = Tournament.objects.filter(category=categ, active=True)
            return render_to_response('standings.html',{'categ':categ_name, 'categ_list':categ_list},\
                              context_instance=RequestContext(request))
    else:
        return render_to_response('standings.html',{'categ':categ,'categ_error':True},\
                              context_instance=RequestContext(request))
        
def add_club(request):
    clubForm = ClubForm(request.POST) 

    if request.method == 'POST':
        clubForm = ClubForm(request.POST)

        if clubForm.is_valid():
            clubForm.save()
            return redirect('club_details')
    else:
        clubForm = ClubForm()

    return render_to_response('club_form.html',{'clubForm':clubForm},context_instance=RequestContext(request))

def view_club(request,clubCode):
    club = Club.objects.get(pk=clubCode)
    return render_to_response('club_view.html',{'club':club},context_instance=RequestContext(request))
